package com.xianggu.demo;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author chengjy
 * @date 2025/4/29
 */
public class JavaSeTest {

    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 1, 2, 9, 4, 7, 6};
        JavaSeTest test = new JavaSeTest();
        System.out.println(test.findSecondSmallest(arr));

        String filePath = "D:\\javaDemo\\xiangGu\\xiangGu\\Springboot-xiangGu\\src\\main\\java\\com\\xianggu\\Collection\\手写实现HashMap";
        String content = test.readFile(filePath);
        System.out.println("读取到文件内容：\n" + content);
    }

    public void quickSort(int[] arr, int low, int high) {
        if (arr == null || arr.length == 0 || low >= high) {
            return;
        }

        int middle = low + (high - low) / 2;
        int pivot = arr[middle];

        int i = low, j = high;
        while (i <= j) {
            while (arr[i] < pivot) {
                i++;
            }
            while (arr[j] > pivot) {
                j--;
            }
            if (i <= j) {
                swap(arr, i, j);
                i++;
                j--;
            }
        }

        if (low < j) {
            quickSort(arr, low, j);
        }
        if (high > i) {
            quickSort(arr, i, high);
        }
    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


    /**
     * 读取文件内容
     *
     * @param filePath
     * @return
     */
    public String readFile(String filePath) {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return content.toString();
    }

    // 1. 找出数组中第二小的元素
    public int findSecondSmallest(int[] arr) {
        if (arr == null || arr.length < 2) {
            throw new IllegalArgumentException("数组长度至少为2");
        }
        // 最小
        int smallest = Integer.MAX_VALUE;
        // 第二小
        int secondSmallest = Integer.MAX_VALUE;

        // 遍历数组arr中的每个元素
        for (int num : arr) {
            // 如果当前元素小于已知的最小值，则更新最小值和第二小值
            if (num < smallest) {
                secondSmallest = smallest;
                smallest = num;
            } else if (num < secondSmallest && num != smallest) {
                // 如果当前元素小于第二小值但不等于最小值，则更新第二小值
                secondSmallest = num;
            }
        }

        return secondSmallest;
    }

    // 2. 字符串反转
    public String reverseString(String str) {
        if (str == null) {
            return null;
        }
        return new StringBuilder(str).reverse().toString();
    }
}
